import QtQuick
import QtQuick.Controls


Item {
    id: root

    // 填充颜色
    property color backgroundColor: "transparent"

    // 边框
    property int borderWidth: 2
    property color borderColor: "#000000"

    // 圆角
    property int radius: 10
    property list<int> radiusList: [radius, radius, radius, radius]

    opacity: 1
    smooth: true

    Canvas {
        id: canvas
        anchors.fill:parent
        smooth: root.smooth

        onPaint: {
            var radiusList = root.radiusList.slice();
            var ratio = 1;
            var halfBorder = root.borderWidth / 2; // 获取半边框宽度

            // 调整可用空间计算，考虑边框宽度
            var availableWidth = width - root.borderWidth;
            var availableHeight = height - root.borderWidth;

            // 检查冲突（考虑边框宽度）
            if (availableWidth / (radiusList[0] + radiusList[1]) < ratio) {
                ratio = availableWidth / (radiusList[0] + radiusList[1]);
            }
            if(availableHeight / (radiusList[1] + radiusList[2]) < ratio) {
                ratio = availableHeight / (radiusList[1] + radiusList[2]);
            }
            if (availableWidth / (radiusList[2] + radiusList[3]) < ratio) {
                ratio = availableWidth / (radiusList[2] + radiusList[3]);
            }
            if(availableHeight / (radiusList[3] + radiusList[0]) < ratio) {
                ratio = availableHeight / (radiusList[3] + radiusList[0]);
            }

            radiusList[0] = radiusList[0] * ratio;
            radiusList[1] = radiusList[1] * ratio;
            radiusList[2] = radiusList[2] * ratio;
            radiusList[3] = radiusList[3] * ratio;

            var ctx = getContext('2d');
            ctx.fillStyle = root.backgroundColor;
            ctx.lineWidth = root.borderWidth;
            ctx.strokeStyle = root.borderWidth;

            // 调整绘制位置，留出边框空间
            var drawX = halfBorder;
            var drawY = halfBorder;
            var drawWidth = width - root.borderWidth;
            var drawHeight = height - root.borderWidth;

            ctx.beginPath();
            ctx.moveTo(drawX + radiusList[0], drawY);
            ctx.lineTo(drawX + drawWidth - radiusList[1], drawY);
            ctx.arc(drawX + drawWidth - radiusList[1], drawY + radiusList[1],
                    radiusList[1], -Math.PI/2, 0);
            ctx.lineTo(drawX + drawWidth, drawY + drawHeight - radiusList[2]);
            ctx.arc(drawX + drawWidth - radiusList[2], drawY + drawHeight - radiusList[2],
                    radiusList[2], 0, Math.PI/2);
            ctx.lineTo(drawX + radiusList[3], drawY + drawHeight);
            ctx.arc(drawX + radiusList[3], drawY + drawHeight - radiusList[3],
                    radiusList[3], Math.PI/2, Math.PI);
            ctx.lineTo(drawX, drawY + radiusList[0]);
            ctx.arc(drawX + radiusList[0], drawY + radiusList[0],
                    radiusList[0], Math.PI, 3*Math.PI/2);
            ctx.closePath();
            ctx.fill();
            ctx.stroke();
        }
    }

    onWidthChanged: canvas.requestPaint()
    onHeightChanged: canvas.requestPaint()
    onBackgroundColorChanged: canvas.requestPaint()
}
